/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;


import Windows.SimpleTableDemo;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.Object;
/**
 *
 * @author DavidUser
 */
public class MyClipBoard implements Runnable {
    protected static boolean b_TimeToStop = false;
    protected static ClipBoardStruct c_ClipStruct;
    protected static SimpleTableDemo c_Table;
    
    public static void  StartMyClipBoardWacher(SimpleTableDemo table){
        c_Table = table;
        (new Thread(new MyClipBoard())).start();
    }
    
    protected static synchronized final boolean GetTimeToStop(){
        return b_TimeToStop;
    }
    
    protected static synchronized final void SetTimeToStop(boolean stopThread){
        b_TimeToStop = stopThread;
    }
    
    public String GetClipBoard() throws UnsupportedFlavorException, IOException {
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String) t.getTransferData(DataFlavor.stringFlavor);
            return text;
        }
        return null;
    }

    public void run() {
        Object lastString = "";
        Object currentString = "";
        c_ClipStruct = new ClipBoardStruct();
        while (true){
            try {
                currentString = this.GetClipBoard();
            } catch (    UnsupportedFlavorException | IOException ex) {
                Logger.getLogger(MyClipBoard.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (!currentString.equals(lastString)){
                lastString = currentString;
                c_ClipStruct.newEntry(currentString);
                SimpleTableDemo.AddNewRows(c_ClipStruct.getEntry());
            }
        }
    }
}
